package com.example.job.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.job.Activity.CollectActivity;
import com.example.job.Activity.LoginActivity;
import com.example.job.Activity.UpdateActivity;
import com.example.job.R;

import com.example.job.entity.UserInfo;


public class mineFragment extends Fragment {
    private View rootView;
    private TextView my_name;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_mine, container, false);
        my_name = rootView.findViewById(R.id.my_name);

        rootView.findViewById(R.id.update).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), UpdateActivity.class);
                        startActivityForResult(intent, 1000);
                    }
                }
        );
        rootView.findViewById(R.id.collection).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CollectActivity.class);
                startActivity(intent);
            }
        });

        return rootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Context context = getContext();

        if (context != null) {
            UserInfo userInfo = UserInfo.getUserInfo(context);

            if (userInfo != null) {
                my_name.setText(userInfo.getUsername());
            } else {
                my_name.setText("未登录");
            }
        }


    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000) {
            getActivity().finish();
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        }
    }


}